package cn.turingbyte.FileIO;

import java.io.*;

public class Mityan20200107 {
    public static void main(String[] args){
        File file = new File("abc.txt");
        if(!file.exists()){
            System.out.println("文件不存在，正在尝试创建新文件");
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            FileWriter fileWriter = new FileWriter(file);
            fileWriter.write("我是mit的严教授，现在给罗老板打工\n我的妈呀");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        try {
//            FileReader fileReader = new FileReader(file);
//            char [] chars = new char[1000];
//            fileReader.read(chars);
//            for(char c :chars){
//                System.out.print(c);
//            }
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String a;
            while((a = bufferedReader.readLine())!=null){
                System.out.println(a);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
